home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / MAKEBUF.CC < prev    next >
C/C++ Source or Header  |  1992-03-29  |  2KB  |  76 lines

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. // Modified for GNU iostream by Per Bothner 1991.
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "%W% (Berkeley) %G%";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include "ioprivate.h"
  25. #include "fstream.h"
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28.  
  29. /*
  30.  * Allocate a file buffer, or switch to unbuffered I/O.
  31.  * Per the ANSI C standard, ALL tty devices default to line buffered.
  32.  *
  33.  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
  34.  * optimisation) right after the _fstat() that finds the buffer size.
  35.  */
  36. int filebuf::doallocate()
  37. {
  38.     if (unbuffered()) {
  39.     setb(_fb._shortbuf, _fb._shortbuf+1, 0);
  40.     return 1;
  41.     }
  42.     register size_t size, couldbetty;
  43.     register char *p;
  44.     struct stat st;
  45.  
  46.     if (fd() < 0 || _fstat(fd(), &st) < 0) {
  47.     couldbetty = 0;
  48.     size = BUFSIZ;
  49. #if 0
  50.     /* do not try to optimise fseek() */
  51.     fp->_flags |= __SNPT;
  52. #endif
  53.     } else {
  54.     couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
  55. #ifdef NO_ST_BLKSIZE
  56.     size = BUFSIZ;
  57. #else
  58.     size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize;
  59. #endif
  60.     }
  61. #ifdef USE_MALLOC_BUF
  62.     if ((p = malloc(size)) == NULL) {
  63.     unbuffered(1);
  64. //    fp->_bf._base = fp->_p = fp->_nbuf;
  65. //    fp->_bf._size = 1;
  66.     return EOF;
  67.     }
  68. #else
  69.     p = ALLOC_BUF(size);
  70. #endif
  71.     setb(p, p+size, 1);
  72.     if (couldbetty && _isatty(fd()))
  73.     _flags |= _S_LINE_BUF;
  74.     return 1;
  75. }
  76.